//
//  HROrderApplyFooter.m
//  haierapp
//
//  Created by gaotangzhang on 14-8-19.
//  Copyright (c) 2014年 haier. All rights reserved.
//

#import "HROrderApplyFooter.h"

#define HEIGHT_SPERATE_VIEW 10.0f
#define HEIGHT_BUTTON_VIEW 40.0f

#define WIDTH_BUTTON 70.0f
#define PADDING_RIGHT 7.0f


@implementation HROrderApplyFooter

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

-(id)initWithShowBottomLine:(BOOL)showLine AndShowSeperateView:(BOOL)showSeperate{
    self=[[[NSBundle mainBundle]loadNibNamed:@"HROrderApplyFooter" owner:self options:nil]objectAtIndex:0];
    
    //设置背景图片
    UIImage *grayImage=[[UIImage imageNamed:@"grey_border"]resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10)];
    UIImage *blueImage=[[UIImage imageNamed:@"blue_border"]resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10)];
    
    [self.returnButton setBackgroundImage:grayImage forState:UIControlStateNormal];
    [self.commentButton setBackgroundImage:grayImage forState:UIControlStateNormal];
    [self.buyAgainButton setBackgroundImage:grayImage forState:UIControlStateNormal];
    [self.progressButton setBackgroundImage:grayImage forState:UIControlStateNormal];
    [self.receiveButton setBackgroundImage:blueImage forState:UIControlStateNormal];
    [self.cancelButton setBackgroundImage:grayImage forState:UIControlStateNormal];
    [self.payButton setBackgroundImage:grayImage forState:UIControlStateNormal];
    
    if (showLine) {
        self.bottomLine.hidden=NO;
    }else{
        self.bottomLine.hidden=YES;
    }
    
    NSLayoutConstraint *svHeightCons;//seperateView的高度
    NSLayoutConstraint *bvHeightCons;//buttonView的高度
    
    for (NSLayoutConstraint *constraint in self.seperateView.constraints) {
        if (constraint.firstAttribute==NSLayoutAttributeHeight && constraint.firstItem==self.seperateView) {
            svHeightCons=constraint;
            break;
        }
    }
    
    for (NSLayoutConstraint *constraint in self.buttonView.constraints) {
        if (constraint.firstAttribute==NSLayoutAttributeHeight && constraint.firstItem==self.buttonView) {
            bvHeightCons=constraint;
            break;
        }
    }
    
    //将buttonView的高度设为0,并隐藏
    bvHeightCons.constant=0.0f;
    self.buttonView.hidden=YES;
    
    if (showSeperate) {
        self.seperateView.hidden=NO;
        svHeightCons.constant=HEIGHT_SPERATE_VIEW;
        
    }else{
        self.seperateView.hidden=YES;
        svHeightCons.constant=0.0f;
    }
    
    return self;
}


-(id)initWithShowBottomLine:(BOOL)showLine AndShowSeperateView:(BOOL)showSeperate andShowButtonByType:(ORDER_TYPE)orderType{
    self = [self initWithShowBottomLine:showLine AndShowSeperateView:showSeperate];
    
    [self showButtonsByOrderType:orderType];
    
    return self;
}

//根据订单类型，显示按钮
-(void)showButtonsByOrderType:(ORDER_TYPE)orderType{
    
    NSLayoutConstraint *bvHeightCons;//buttonView的高度
    
    
    for (NSLayoutConstraint *constraint in self.buttonView.constraints) {
        if (constraint.firstAttribute==NSLayoutAttributeHeight && constraint.firstItem==self.buttonView) {
            bvHeightCons=constraint;
            break;
        }
    }
    
    if (orderType!=ORDER_TYPE_NONE) {
        //显示buttonView，并设置高度
        self.buttonView.hidden=NO;
        bvHeightCons.constant=HEIGHT_BUTTON_VIEW;
        
        //隐藏所有按钮
        [self hideAllButton];
        
        BOOL commentFlag=[self orderHadComment];//是否已评价
        
        switch (orderType) {
            case ORDER_TYPE_WAIT_FOR_PAY:
                //待付款[取消订单、付款]
                self.cancelButton.hidden=NO;
                self.cancelWidthCons.constant=WIDTH_BUTTON;
                self.cancelPaddingRight.constant=PADDING_RIGHT;
                
                self.payButton.hidden=NO;
                self.payWidthCons.constant=WIDTH_BUTTON;
                
                break;
            case ORDER_TYPE_WAIT_FOR_DELIVERY:
                //待发货[取消订单]
                self.progressButton.hidden=NO;
                self.progressWidthCons.constant=WIDTH_BUTTON;
                self.progressPaddingRight.constant=PADDING_RIGHT;
                
                self.cancelButton.hidden=NO;
                self.cancelWidthCons.constant=WIDTH_BUTTON;
                self.cancelPaddingRight.constant=PADDING_RIGHT;
                
                break;
            case ORDER_TYPE_WAIT_FOR_RECEIPT:
                //待收货[查看进度、确认收货]
                self.progressButton.hidden=NO;
                self.progressWidthCons.constant=WIDTH_BUTTON;
                self.progressPaddingRight.constant=PADDING_RIGHT;
                
                self.receiveButton.hidden=NO;
                self.receiveWidthCons.constant=WIDTH_BUTTON;
                self.receivePaddingRight.constant=PADDING_RIGHT;
                
                break;
            case ORDER_TYPE_RECEIPTED:
                //已收货[退货申请、再次购买、评价商品]
                self.returnButton.hidden=NO;
                self.returnPaddingRight.constant=PADDING_RIGHT;
                
                self.buyAgainButton.hidden=NO;
                self.buyAgainWidthCons.constant=WIDTH_BUTTON;
                self.buyAgainPaddingRight.constant=PADDING_RIGHT;
                
                
                if (!commentFlag) {
                    self.commentButton.hidden=NO;
                    self.commentPaddingRight.constant=PADDING_RIGHT;
                    self.commentWidthCons.constant=WIDTH_BUTTON;
                }
                break;

            case ORDER_TYPE_SUCCEED:
                //交易成功[查看进度、再次购买、订单评价]
                self.progressButton.hidden=NO;
                self.progressWidthCons.constant=WIDTH_BUTTON;
                self.progressPaddingRight.constant=PADDING_RIGHT;
                
                self.buyAgainButton.hidden=NO;
                self.buyAgainWidthCons.constant=WIDTH_BUTTON;
                self.buyAgainPaddingRight.constant=PADDING_RIGHT;
                
                if (!commentFlag) {
                    self.commentButton.hidden=NO;
                    self.commentPaddingRight.constant=PADDING_RIGHT;
                    self.commentWidthCons.constant=WIDTH_BUTTON;
                }
                break;
                
            case ORDER_TYPE_CLOSED:
                //交易关闭[再次购买]
                self.buyAgainButton.hidden=NO;
                self.buyAgainWidthCons.constant=WIDTH_BUTTON;
                self.buyAgainPaddingRight.constant=PADDING_RIGHT;
                break;
            case ORDER_TYPE_AFTERSALE_DEALING:
                //售后处理
                self.buyAgainButton.hidden=NO;
                self.buyAgainWidthCons.constant=WIDTH_BUTTON;
                self.buyAgainPaddingRight.constant=PADDING_RIGHT;
                break;
                
            case ORDER_TYPE_WAIT_COMMENT:
                //待评价
                self.buyAgainButton.hidden=NO;
                self.buyAgainWidthCons.constant=WIDTH_BUTTON;
                self.buyAgainPaddingRight.constant=PADDING_RIGHT;
                
                self.progressButton.hidden=NO;
                self.progressWidthCons.constant=WIDTH_BUTTON;
                self.progressPaddingRight.constant=PADDING_RIGHT;
                
                if (!commentFlag) {
                    self.commentButton.hidden=NO;
                    self.commentPaddingRight.constant=PADDING_RIGHT;
                    self.commentWidthCons.constant=WIDTH_BUTTON;
                }
                
                break;
            case ORDER_TYPE_CANCELED:
                //已取消[再次购买]
                self.buyAgainButton.hidden=NO;
                self.buyAgainWidthCons.constant=WIDTH_BUTTON;
                self.buyAgainPaddingRight.constant=PADDING_RIGHT;
                
            default:
                break;
        }
        
    }
    
}

//隐藏所有按钮
-(void)hideAllButton{
    self.returnButton.hidden=YES;
    
    self.commentButton.hidden=YES;
    self.commentWidthCons.constant=0.0f;
    self.commentPaddingRight.constant=0.0f;
    
    self.buyAgainButton.hidden=YES;
    self.buyAgainWidthCons.constant=0.0f;
    self.buyAgainPaddingRight.constant=0.0f;
    
    self.progressButton.hidden=YES;
    self.progressWidthCons.constant=0.0f;
    self.progressPaddingRight.constant=0.0f;
    
    
    self.receiveButton.hidden=YES;
    self.receiveWidthCons.constant=0.0f;
    self.receivePaddingRight.constant=0.0f;
    
    self.cancelButton.hidden=YES;
    self.cancelWidthCons.constant=0.0f;
    self.cancelPaddingRight.constant=0.0f;
    
    self.payButton.hidden=YES;
    self.payWidthCons.constant=0.0f;
}

//是否已评价
-(BOOL)orderHadComment{
    
    BOOL flag=NO;//未评价
    
    for (HROrderProductInfo *product in _order.productList) {
        if ([product.isEvaluate isEqualToString:@"1"]) {
            flag=YES;//已评价
            break;
        }
    }
    return flag;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

#pragma mark - Button Tap Action

//申请退换货
- (IBAction)onTapReturnOrder:(id)sender {
    if ([self.delegate respondsToSelector:@selector(returnsOrderProduct:)]) {
        [self.delegate returnsOrderProduct:self.order];
    }
}

//评价订单
- (IBAction)onTapCommentOrder:(id)sender {
    if ([self.delegate respondsToSelector:@selector(commentOrderProduct:)]) {
        [self.delegate commentOrderProduct:self.order];
    }
}

//再次购买
- (IBAction)onTapBuyAgain:(id)sender {
    if ([self.delegate respondsToSelector:@selector(buyAgainOrderProduct:)]) {
        [self.delegate buyAgainOrderProduct:self.order];
    }
}

//查看进度
- (IBAction)onTapViewProgress:(id)sender {
    
    if ([self.delegate respondsToSelector:@selector(viewOrderProgressByOrderId:)]) {
        [self.delegate viewOrderProgressByOrderId:self.orderNo];
    }
    
}

//确认收货
- (IBAction)onTapReceive:(id)sender {
    
    if ([self.delegate respondsToSelector:@selector(receiveProductsByOrderId:)]) {
        [self.delegate receiveProductsByOrderId:self.orderNo];
    }
}

//取消订单
- (IBAction)onTapCancel:(id)sender {
    
    if ([self.delegate respondsToSelector:@selector(cancelOrderByOrderId:)]) {
        [self.delegate cancelOrderByOrderId:self.orderNo];
    }
    
}

//付款
- (IBAction)onTapPayment:(id)sender {
    
    if ([self.delegate respondsToSelector:@selector(paymentOrderByOrderId:)]) {
        [self.delegate paymentOrderByOrderId:self.section];
    }
}
@end
